package com.asman.vr.utils

import android.content.Context
import androidx.appcompat.app.AlertDialog
import com.asman.vr.R

/**
 * 弹框-统一样式：PositiveButton 橙色，AppTheme已经设置，NegativeButton #494949色
 * Created by yubaokang on 16-7-23.
 */
object DialogUtils {

    /**
     * @param context
     * @param title       标题
     * @param negativeTxt 否定性质的操作
     * @param positiveTxt 确定性质的操作
     * @param listener
     */
    fun showDialog(
        context: Context,
        title: String? = null,
        negativeTxt: String? = null,
        positiveTxt: String? = null,
        listener: ButtonClickListener? = null,
        cancelable: Boolean = true
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setMessage(title).setCancelable(cancelable)
            .setNegativeButton(negativeTxt) { dialog, id ->
                dialog.cancel()
                listener?.negativeButton()
            }.setPositiveButton(positiveTxt) { dialog, id ->
                dialog.cancel()
                listener?.positiveButton()
            }
        val alert = builder.create()
        alert.show()
        //        AlertDialog创建之后才可以修改Button信息
        alert.getButton(AlertDialog.BUTTON_NEGATIVE)
            .setTextColor(context.getMyColor(R.color._333333))
        alert.getButton(AlertDialog.BUTTON_POSITIVE)
            .setTextColor(context.getMyColor(R.color.colorPrimary))
    }

    fun showDialog(
        context: Context,
        title: String,
        msg: String,
        negativeTxt: String,
        positiveTxt: String,
        listener: ButtonClickListener
    ) {
        showDialog(context, title, msg, negativeTxt, positiveTxt, true, listener)
    }

    fun showDialog(
        context: Context,
        title: String,
        msg: String,
        negativeTxt: String?,
        positiveTxt: String?,
        cancelable: Boolean,
        listener: ButtonClickListener
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setMessage(msg).setTitle(title).setCancelable(cancelable)
        if (!negativeTxt.isNullOrEmpty()) {
            builder.setNegativeButton(negativeTxt) { dialog, id ->
                dialog.cancel()
                listener.negativeButton()
            }
        }
        if (!positiveTxt.isNullOrEmpty()) {
            builder.setPositiveButton(positiveTxt) { dialog, id ->
                dialog.cancel()
                listener.positiveButton()
            }
        }

        val alert = builder.create()
        alert.show()
        //AlertDialog创建之后才可以修改Button信息
        alert.getButton(AlertDialog.BUTTON_NEGATIVE)
            .setTextColor(context.getMyColor(R.color._333333))
        alert.getButton(AlertDialog.BUTTON_POSITIVE)
            .setTextColor(context.getMyColor(R.color.colorPrimary))
    }

    /**
     * Dialog 3个按钮 从左到右依次顺序：中性-消极-积极（取消-不通过-合格）
     *
     * @param neutralTxt  中性  取消等
     * @param negativeTxt 消极：不同意，不满意，不合格等；
     * @param positiveTxt 积极：合格，满意，同意，确定
     */
    fun showThreeButtonDialog(
        context: Context,
        title: String,
        msg: String,
        neutralTxt: String,
        negativeTxt: String,
        positiveTxt: String,
        listener: ButtonClickListener
    ) {
        showThreeButtonDialog(
            context, title, msg, neutralTxt, negativeTxt, positiveTxt, true, listener
        )
    }

    fun showThreeButtonDialog(
        context: Context,
        title: String,
        msg: String,
        neutralTxt: String,
        negativeTxt: String,
        positiveTxt: String,
        cancelable: Boolean,
        listener: ButtonClickListener
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setMessage(msg).setTitle(title).setCancelable(cancelable)
            .setNeutralButton(neutralTxt) { dialog, id ->
                dialog.cancel()
                listener.neutralButton()
            }.setNegativeButton(negativeTxt) { dialog, id ->
                dialog.cancel()
                listener.negativeButton()
            }.setPositiveButton(positiveTxt) { dialog, id ->
                dialog.cancel()
                listener.positiveButton()
            }
        val alert = builder.create()
        alert.show()
        //        AlertDialog创建之后才可以修改Button信息
        alert.getButton(AlertDialog.BUTTON_NEGATIVE)
            .setTextColor(context.getMyColor(R.color._333333))
        alert.getButton(AlertDialog.BUTTON_POSITIVE)
            .setTextColor(context.getMyColor(R.color.colorPrimary))
    }


    fun showDialogOneBtn(
        context: Context,
        title: String,
        msg: String,
        positiveTxt: String,
        cancelable: Boolean,
        listener: ButtonClickListener
    ) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title).setMessage(msg).setCancelable(cancelable)
            .setPositiveButton(positiveTxt) { dialog, id ->
                dialog.cancel()
                listener.positiveButton()
            }
        val alert = builder.create()
        alert.show()
        alert.getButton(AlertDialog.BUTTON_POSITIVE)
            .setTextColor(context.getMyColor(R.color.colorPrimary))
    }

    interface ButtonClickListener {
        fun neutralButton() //中性  取消等

        fun negativeButton() //消极：不同意，不满意，不合格等；

        fun positiveButton() //积极：合格，满意，同意，确定
    }

    abstract class ButtonClickListenerAbstract : ButtonClickListener {
        override fun negativeButton() {}

        override fun neutralButton() {}
    }
}
